import { Component, OnInit} from '@angular/core';
import { Router } from '@angular/router';
import { LocalStorageService} from '../shared';
declare let $: any;
@Component({
  selector: 'app-login',
  templateUrl: './login.component.html'
})
export class LoginComponent implements OnInit{
  title = 'login';
  password: string ='';
  username: string ='';
  userInfos: any;
  userInfo: any;
  constructor(private router: Router,
              private localStorageService:　LocalStorageService) {
      this.userInfos = [
          {username: '003', password: '003', roleId: '0', name: 'jack'},
          {username: '002', password: '002', roleId: '1', name: 'tom'},
          {username: '001', password: '001', roleId: '1', name: 'lisa'},
      ]

  }

  ngOnInit() {

    //ajax  post请求数据
    // $('')`

    //         //保存cookie  path='/'整个主机下的页面都可访问
    //         $.cookie()
  }
  login() {
      this.userInfo = this.userInfos.find(v => v.username === this.username)
      if ( this.userInfo && this.userInfo.username === this.username && this.userInfo.password === this.password) {
          this.localStorageService.writeObject('userinfo', this.userInfo);
          this.goHome();
      }
  }
  goHome() {
    this.router.navigate(['/home']);
  }

}
